package com.ybxx.dashboard.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;

import com.ybxx.base.model.ReadPerson;
import com.ybxx.base.model.Residents;
import com.ybxx.base.model.Room;
import com.ybxx.base.service.ReadPersonService;
import com.ybxx.base.service.ResidentsService;
import com.ybxx.core.command.QueryFilter;
import com.ybxx.core.util.DateUtil;
import com.ybxx.core.util.UUIDGenerator;
import com.ybxx.core.web.action.BaseAction;
import com.ybxx.dashboard.model.CopyFormVO;
import com.ybxx.dashboard.model.ReadFormVO;
import com.ybxx.dashboard.model.Spread;
import com.ybxx.dashboard.model.Watermeter;
import com.ybxx.dashboard.model.WatermeterRoom;
import com.ybxx.dashboard.service.SpreadService;
import com.ybxx.dashboard.service.WatermeterService;
import com.ybxx.system.SystemConstants;
import com.ybxx.system.model.User;


public class WatermeterAction extends BaseAction {
	@Resource
	private WatermeterService watermeterService;
	@Resource
	private SpreadService spreadService;
	@Resource
	private ReadPersonService readPersonService;
	@Resource
	private ResidentsService residentsService;	
		
	private List<Watermeter> watermeters;
	private List<Spread> spreads;
	private List<ReadPerson> readPersons;
	private	List<Residents> residentsex;
	private List<String> cbnames;
	private String rid;
	private Watermeter watermeter;
	private String msg="succ";
	private String rcodes;
	private String rids;
  	private String rnames;
  	private String rnums;
  	private String compares;
  	private String contents;
  	private String roomcodes;
  	private Map<String,List<CopyFormVO>> map;
  	private List<ReadFormVO> vos;
  	private String cid;
	private String bid;
	private String uid;
	private String metertype;
	private String printtype;
	private String sorttype;
	private String personid;
	private String unitid;
	private String personName;
	private String unitName;
	private String ymonth;
	private String ctime;
	private String currentTime;
	private String currentMonth;
	public String initPage(){
		currentTime=DateUtil.formatDate(new Date());
		currentMonth=DateUtil.addMonthFormDate(currentTime, -1);
		return "initPage";
	}
	/*保存电表读数*/
	public String saveRead(){
		try{
			/**获取当前登录者*/
			User user=(User)super.getSession().getAttribute(SystemConstants.SESSION_CURRENT_USER);
			watermeterService.saveRead(ctime,ymonth,vos,user.getLoginid());
		}catch(Exception ex){
			msg="水表读数保存失败";
		}finally{
			return "succ";
		}
	}
	public String queryReadForm(){
		vos=watermeterService.queryReadForm(printtype,sorttype,personid,unitid,ymonth);
		return "queryReadForm";
	}
	public String queryCopyForm(){
		map=watermeterService.queryCopyForm(printtype,sorttype,personid,unitid);
		cbnames=new ArrayList<String>();
		cbnames.addAll(map.keySet());
		return "queryCopyForm";
	}
	/*加载查看的数据*/
	public String view(){
		spreads=spreadService.getAll();
		readPersons=readPersonService.getAll();
		if(watermeter!=null && StringUtils.isNotEmpty(watermeter.getWmcode())){
			watermeter=watermeterService.load(watermeter.getWmcode());
		}
		return "view";
	}
	/*删除水表信息*/
	public String remove(){
		try{
			watermeterService.remove(watermeter.getWmcode());
		}catch(Exception ex){
			msg="水表信息删除失败！";
		}finally{
			return "succ";
		}
	}
	/*保存水表信息*/
	public String save(){
		try{
			if(watermeter.getWmcode()==null || watermeter.getWmcode().equals("")){
				watermeter.setWmcode(UUIDGenerator.getUUID());
			}
			String[] rcode=rcodes.split(",");
			String[] rid=rids.split(",");
			String[] rname=rnames.split(",");
			String[] rnum=rnums.split(",");
			String[] compare=compares.split(",");
			String[] content=contents.split(",");
			String[] roomcode=roomcodes.split(",");
			for(int i=0;i<rcode.length;i++){
				WatermeterRoom watermetermeterRoom=new WatermeterRoom();
				watermetermeterRoom.setWatermeter(watermeter);
				watermetermeterRoom.setWmid(UUIDGenerator.getUUID());
				watermetermeterRoom.setCompare(compare.length<=i?"0":compare[i]);
				watermetermeterRoom.setContent(content.length<=i?null:content[i]);
				watermetermeterRoom.setPname(rname[i]);
				watermetermeterRoom.setPnum(rnum[i]);
				watermetermeterRoom.setResidents(new Residents(rcode[i]));
				watermetermeterRoom.setRoom(new Room(rid[i]));
				watermetermeterRoom.setRoomcode(roomcode[i]);
				watermeter.getWatermeterRooms().add(watermetermeterRoom);
			}
			watermeterService.save(watermeter);
		}catch(Exception ex){
			ex.printStackTrace();
			msg="水表信息维护失败";
		}finally{
			return "succ";
		}
	}
	/*加载住户信息*/
	public String loadResidents(){
		residentsex=residentsService.loadAnmeterResidents(rid.substring(1));
		return "loadResidents";
	}
	/*加载新增，修改的数据*/
	public String load(){
		spreads=spreadService.getAll();
		readPersons=readPersonService.getAll();
		if(watermeter!=null && StringUtils.isNotEmpty(watermeter.getWmcode())){
			watermeter=watermeterService.load(watermeter.getWmcode());
		}
		return "load";
	}
	/*初始化管理页面*/
	public String init(){
		QueryFilter filter=new QueryFilter(super.getRequest());
		watermeters=watermeterService.queryPage(filter,cid,bid,uid,rid);
		super.setRownum(filter.getPagingBean().getTotalItems());
		super.setCurrentpage(Integer.parseInt(filter.getPagingBean().getStart()+"")/Integer.parseInt(filter.getPagingBean().getPageSize()+"")+1);
		super.setPageSize(Integer.parseInt(filter.getPagingBean().getPageSize()+""));
		return "init";
	}
	public List<Watermeter> getWatermeters() {
		return watermeters;
	}
	public void setWatermeters(List<Watermeter> watermeters) {
		this.watermeters = watermeters;
	}
	public List<Spread> getSpreads() {
		return spreads;
	}
	public void setSpreads(List<Spread> spreads) {
		this.spreads = spreads;
	}
	public List<ReadPerson> getReadPersons() {
		return readPersons;
	}
	public void setReadPersons(List<ReadPerson> readPersons) {
		this.readPersons = readPersons;
	}
	public List<Residents> getResidentsex() {
		return residentsex;
	}
	public void setResidentsex(List<Residents> residentsex) {
		this.residentsex = residentsex;
	}
	public String getRid() {
		return rid;
	}
	public void setRid(String rid) {
		this.rid = rid;
	}
	public Watermeter getWatermeter() {
		return watermeter;
	}
	public void setWatermeter(Watermeter watermeter) {
		this.watermeter = watermeter;
	}
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	public String getRcodes() {
		return rcodes;
	}
	public void setRcodes(String rcodes) {
		this.rcodes = rcodes;
	}
	public String getRids() {
		return rids;
	}
	public void setRids(String rids) {
		this.rids = rids;
	}
	public String getRnames() {
		return rnames;
	}
	public void setRnames(String rnames) {
		this.rnames = rnames;
	}
	public String getRnums() {
		return rnums;
	}
	public void setRnums(String rnums) {
		this.rnums = rnums;
	}
	public String getCompares() {
		return compares;
	}
	public void setCompares(String compares) {
		this.compares = compares;
	}
	public String getContents() {
		return contents;
	}
	public void setContents(String contents) {
		this.contents = contents;
	}
	public String getRoomcodes() {
		return roomcodes;
	}
	public void setRoomcodes(String roomcodes) {
		this.roomcodes = roomcodes;
	}
	public String getCid() {
		return cid;
	}
	public void setCid(String cid) {
		this.cid = cid;
	}
	public String getBid() {
		return bid;
	}
	public void setBid(String bid) {
		this.bid = bid;
	}
	public String getUid() {
		return uid;
	}
	public void setUid(String uid) {
		this.uid = uid;
	}
	public List<String> getCbnames() {
		return cbnames;
	}
	public void setCbnames(List<String> cbnames) {
		this.cbnames = cbnames;
	}
	public Map<String, List<CopyFormVO>> getMap() {
		return map;
	}
	public void setMap(Map<String, List<CopyFormVO>> map) {
		this.map = map;
	}
	public String getMetertype() {
		return metertype;
	}
	public void setMetertype(String metertype) {
		this.metertype = metertype;
	}
	public String getPrinttype() {
		return printtype;
	}
	public void setPrinttype(String printtype) {
		this.printtype = printtype;
	}
	public String getSorttype() {
		return sorttype;
	}
	public void setSorttype(String sorttype) {
		this.sorttype = sorttype;
	}
	public String getPersonid() {
		return personid;
	}
	public void setPersonid(String personid) {
		this.personid = personid;
	}
	public String getUnitid() {
		return unitid;
	}
	public void setUnitid(String unitid) {
		this.unitid = unitid;
	}
	public String getPersonName() {
		return personName;
	}
	public void setPersonName(String personName) {
		this.personName = personName;
	}
	public String getUnitName() {
		return unitName;
	}
	public void setUnitName(String unitName) {
		this.unitName = unitName;
	}
	public List<ReadFormVO> getVos() {
		return vos;
	}
	public void setVos(List<ReadFormVO> vos) {
		this.vos = vos;
	}
	public String getYmonth() {
		return ymonth;
	}
	public void setYmonth(String ymonth) {
		this.ymonth = ymonth;
	}
	public String getCtime() {
		return ctime;
	}
	public void setCtime(String ctime) {
		this.ctime = ctime;
	}
	public String getCurrentTime() {
		return currentTime;
	}
	public void setCurrentTime(String currentTime) {
		this.currentTime = currentTime;
	}
	public String getCurrentMonth() {
		return currentMonth;
	}
	public void setCurrentMonth(String currentMonth) {
		this.currentMonth = currentMonth;
	}
	
	
}
